<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <!-- 左侧  el-row包裹是为了方便布局-->
            <el-row style="height: 60px">
              <el-button
                @click="addRole"
                icon="el-icon-plus"
                size="small"
                type="primary"
              >
                新增角色
              </el-button>
            </el-row>
            <!-- 表格 -->
            <!-- 绑定数据 prop 是每列显示的是啥 -->
            <el-table :data="list" border="">
              <!-- 列 header-align="right" 单独设置表头-->
              <el-table-column
                align="center"
                type="index"
                width="120"
                label="序号"
              >
              </el-table-column>
              <el-table-column
                align="center"
                prop="name"
                width="240"
                label="名称"
              >
              </el-table-column>
              <el-table-column prop="description" label="描述">
              </el-table-column>
              <el-table-column align="center" label="操作">
                <template slot-scope="{ row }">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(row.id)"
                    >编辑</el-button
                  >
                  <el-button size="small" type="danger" @click="delRole(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页器 -->
            <el-row
              type="flex"
              justify="end"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页器的配置 当前页 每页大小 总数 （这样才能算出） -->
              <!-- current-change 事件 当页码发生变化的时候 -->
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="pagechange"
                layout="prev, pager, next"
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
          <!-- 右侧 -->
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              :closable="false"
              type="info"
              show-icon
            >
            </el-alert>
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="企业名称">
                <el-input
                  v-model="FormData.name"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="企业地址">
                <el-input
                  v-model="FormData.companyAddress"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="电话">
                <el-input
                  v-model="FormData.companyPhone"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="FormData.mailbox"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="FormData.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增或者编辑弹层对话框 -->
    <el-dialog :title="showText" :visible="showDialog" @close="cancel">
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description"></el-input>
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="end">
        <el-col :span="8">
          <el-button @click="cancel" size="small"> 取消 </el-button>
          <el-button @click="determine" type="primary" size="small">
            确定
          </el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限的弹出层  showPermDialog-->
    <el-dialog title="分配权限" :visible="showPermDialog" >
   <!-- el-tree -->
   <el-tree 
   ref="permTree"
   :data="permData" 
   :props="defaultProps"
    :default-expand-all="true"
    :show-checkbox="true"
    :check-strictly="true"
    :default-checked-keys="selectCheck"
        node-key="id"
   ></el-tree>
      <el-row type="flex" justify="end">
        <el-col :span="8">
          <el-button @click="btnPermCancel"  size="small"> 取消 </el-button>
          <el-button @click="btnPermOk" type="primary" size="small">
            确定
          </el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRoleList,
  getcompany,
  addRole,
  deleteRole,
  getRoleDetail,
  updateRole,
  assignPerm
} from "@/api/setting.js";
import { getPermissionList } from "@/api/permisson";
import { tranListToTreeData } from "@/utils";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      list: [], //表格数据
      //分页器
      page: {
        page: 1, //当前页
        pagesize: 10, //页码大小
        total: 0, //总数
      },
      FormData: {},
      permData:[],//tree的数据 要树形的
      selectCheck:[],//树形结构 默认勾选的
      roleId:'',
      defaultProps:{
        label:'name'
      },
      // 弹层
      showDialog: false,
      showPermDialog:false,// 
      roleForm: {
        name: "",
        description: "",
      },
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters["companyId"],
    showText(){
  return this.roleForm.id ? '编辑部门':'新增部门角色'
}
  },
  created() {
    this.getRoleList();
    this.getcompany();
  },
  methods: {
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page);
      this.list = rows;
      this.page.total = total;
    },
    //页码改变
    pagechange(newpage) {
      this.page.page = newpage;
      this.getRoleList();
    },
    async getcompany(companyId) {
      const data = await getcompany(companyId);
      this.FormData = data;
      // console.log(data);
    },
    async delRole(id) {
      // 表格通过作用域插槽 得到点击的对象
      // console.log(id);
      try {
        await this.$confirm("确认删除？");
        await deleteRole(id);
        this.getRoleList();
        this.$message.success("删除成功");
      } catch (err) {
        console.log(err);
      }
    },
    // 编辑按钮
    async editRole(id) {
      // 数据回传显示
      this.roleForm = await getRoleDetail(id);
      // 显示弹层
      this.showDialog = true;
    },
    // 新增
    addRole() {
      this.showDialog = true;
    },
    // 取消
    cancel() {
      // 对话框的X close 绑定了cancel 事件  而 关闭弹出 会触发close 事件
      // 间接的 确定 -》关闭弹出-》close-》cancel 
      // 所以确定不需要重置表单数据
      (this.roleForm = {}), //清空数据
      this.$refs.roleForm.resetFields(); //重置表单
      this.showDialog = false;
    },
    // 确认
    async determine() {
      try {
        await this.$refs.roleForm.validate();
        if (this.roleForm.id) {
          // 编辑业务
          await updateRole(this.roleForm);
        } else {
          // 新增
          await addRole(this.roleForm);
        }
        this.getRoleList();
        this.$message.success("操作成功");
        this.showDialog = false;
      } catch (err) {
        console.log(err);
      }
    },
    // 分配权限 弹出个层
   async assignPerm(id){
        const {permIds}= await getRoleDetail(id)
        this.selectCheck=permIds
      this.permData=tranListToTreeData(await getPermissionList(),'0')
      this.roleId=id
       this.showPermDialog=true
    },
   async  btnPermOk(){
try {
  // console.log(this.$refs.permTree.getCheckedKeys());
  // console.log( this.roleId );
        // 确定 要提交数据 this.$refs.permTree.getCheckedKys() el-tree的方法 我们选择的tree节点
await assignPerm({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
this.$message.success('分配成功')
this.showPermDialog=false
} catch (err) {
  console.log(err);
  this.$message.success('分配失败')
}
    }
    ,
    btnPermCancel(){
this.selectCheck=[]
this.showPermDialog=false
    }

  },
};
</script>

<style>
</style>
